package ui;

import java.awt.BorderLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import Work.*;
/*读者增删改*/
public class ReaderOther extends JFrame implements ActionListener {
	JTextField jtf1, jtf2, jtf3, jtf4, jtf5;
	private JButton updater_name;
	private JButton updater_number;
	private JButton updateinstitute;
	private JButton updater_amount;
	private JButton returnbutton;

	//创建数据库连接类对象
    Jdbc db = new Jdbc();
	// 添加读者
	public void AddReader() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(4, 2, 5, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("添加读者"));
		JLabel jl2 = new JLabel("读者姓名:");
		jtf2 = new JTextField(10);
		JLabel jl3 = new JLabel("读者电话:");
		jtf3 = new JTextField(10);
		JLabel jl4 = new JLabel("所在学院:");
		jtf4 = new JTextField(10);
		JLabel jl5 = new JLabel("借阅数量:");
		jtf5 = new JTextField(10);
		jp1.add(jl2);
		jp1.add(jtf2);
		jp1.add(jl3);
		jp1.add(jtf3);
		jp1.add(jl4);
		jp1.add(jtf4);
		jp1.add(jl5);
		jp1.add(jtf5);
		jpc.add(jp1);// 把面板jp1加到流式布局上
		add(jpc);// 添加流式布局
		// add(jpc, BorderLayout.CENTER);

		JButton jb1 = new JButton("提交");
		jb1.addActionListener(this);
		jb1.setActionCommand("add");// 没看懂

		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		returnbutton.setActionCommand("no");

		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("读者添加界面");
		setSize(280, 215);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}

	// 删除读者
	public void DeleteReader() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(4, 2, 5, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("删除读者（任选其一输入）"));
		JLabel jl1 = new JLabel("读者编号:");
		jtf1 = new JTextField(10);
		JLabel jl2 = new JLabel("读者姓名:");
		jtf2 = new JTextField(10);
		JLabel jl3 = new JLabel("读者电话:");
		jtf3 = new JTextField(10);
		JLabel jl4 = new JLabel("所在学院:");
		jtf4 = new JTextField(10);
		jp1.add(jl1);// 在面板jp1上添加标签jl1
		jp1.add(jtf1);// 在面板jp1上添加输入框
		jp1.add(jl2);
		jp1.add(jtf2);
		jp1.add(jl3);
		jp1.add(jtf3);
		jp1.add(jl4);
		jp1.add(jtf4);
		jpc.add(jp1);// 把面板jp1加到流式布局上
		add(jpc);// 添加流式布局
		// add(jpc, BorderLayout.CENTER);

		JButton jb1 = new JButton("提交");
		jb1.addActionListener(this);
		jb1.setActionCommand("delete");// 没看懂

		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		returnbutton.setActionCommand("no");

		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("读者删除界面");
		setSize(280, 215);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}

	// 修改读者
	public void UpdateReaderButton() {
		JFrame jFrame = new JFrame("修改部分选择");
		// JPanel jpc = new JPanel();// 默认流式布局
		jFrame.setLayout(new BorderLayout());
		// jFrame.setSize(300, 200);
		JPanel jp1 = new JPanel(new GridLayout(2, 2, 10, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("读者修改"));
		// this.setLayout(new FlowLayout());
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		setTitle("读者修改系统功能选项");
		setSize(300, 200);
		setLocationRelativeTo(null);

		// 监听右上角的退出按钮
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent windowEvent) {
				System.exit(0);
			}
		});
		updater_name = new JButton("修改读者姓名");
		updater_number = new JButton("修改读者电话");
		updateinstitute = new JButton("修改读者所在学院");
		updater_amount = new JButton("修改读者借阅数量");

		jp1.add(updater_name);
		jp1.add(updater_number);
		jp1.add(updateinstitute);
		jp1.add(updater_amount);
		JPanel jp2 = new JPanel();
		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);
		getContentPane().add(jp1, BorderLayout.CENTER);

		updater_name.addActionListener(this);
		updater_number.addActionListener(this);
		updateinstitute.addActionListener(this);
		updater_amount.addActionListener(this);

		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		ReaderOther readerother = new ReaderOther();
		ReaderOther1 readerother1 = new ReaderOther1();
		ForeignKey foreignkey = new ForeignKey();
        ChangeReader changereader = new ChangeReader();
        ArrayList<Reader> readerlist = changereader.read(db);
		if (e.getSource() == updater_name) {//修改
			this.dispose();
			readerother1.Updater_name();
		}else if (e.getSource() == updater_number) {
			this.dispose();
			readerother1.Updater_number();
		}else if (e.getSource() == updateinstitute) {
			this.dispose();
			readerother1.Updateinstitute();
		}else if (e.getSource() == updater_amount) {
			this.dispose();
			readerother1.Updater_amount();
		}else if (e.getActionCommand().equals("add")) {//增加
			String s2 = jtf2.getText().trim();
			String s3 = jtf3.getText().trim();
			String s4 = jtf4.getText().trim();
			String s5 = jtf5.getText().trim();
			if ( s2.equals("") || s3.equals("") || s4.equals("") || s5.equals("")) {
				JOptionPane.showMessageDialog(this, "内容缺失，请检查是否填写完毕全部信息");
				//!!!查重
			} else {
				db.uiinsertreader(db,s2,s3,s4,s5);
				JOptionPane.showMessageDialog(this, "读者姓名:" + s2 +" 读者电话:" + s3+"\n所在学院:" + s4+"借阅数量:" + s5 + "\n读者信息添加成功");
				this.dispose();
			}
		}else if (e.getActionCommand().equals("delete")) {//删除
			String s1 = jtf1.getText().trim();
			String s2 = jtf2.getText().trim();
			String s3 = jtf3.getText().trim();
			String s4 = jtf4.getText().trim();
			if (s2.equals("") && s3.equals("")&&s4.equals("")) {
				if (!foreignkey.FindR_ID(readerlist, s1)) {
					JOptionPane.showMessageDialog(this, "查无此人，请重新输入！");
					readerother.DeleteReader();
				}
				db.uideletereader(db, s1, 1);
				JOptionPane.showMessageDialog(this, "读者编号为 " + s1 + " 的读者信息已删除");
				this.dispose();
			} else if (s1.equals("") && s3.equals("")&& s4.equals("")) {
				if (!foreignkey.FindR_NAME(readerlist, s2)) {
					JOptionPane.showMessageDialog(this, "查无此人，请重新输入！");
					readerother.DeleteReader();
				}
				db.uideletereader(db, s2, 2);
				JOptionPane.showMessageDialog(this, "读者姓名为 " + s2 + " 的读者信息已删除");
				this.dispose();
			} else if (s1.equals("") && s2.equals("") && s4.equals("")) {
				if (!foreignkey.FindR_NUMBER(readerlist, s3)) {
					JOptionPane.showMessageDialog(this, "查无此人，请重新输入！");
					readerother.DeleteReader();
				}
				db.uideletereader(db, s3, 3);
				JOptionPane.showMessageDialog(this, "读者电话为 " + s1 + " 的读者信息已删除");
				this.dispose();
			} else if (s1.equals("") && s2.equals("") && s3.equals("")) {
				if (!foreignkey.FindR_INSTITUTE(readerlist, s4)) {
					JOptionPane.showMessageDialog(this, "查无此人，请重新输入！");
					readerother.DeleteReader();
				}
				db.uideletereader(db, s4, 4);
				JOptionPane.showMessageDialog(this, "所在学院为 " + s1 + " 的读者信息已删除");
				this.dispose();
			}
		}else if (e.getSource() == returnbutton) {
			this.dispose();
			new LibrarianOp();
		} else {
			jtf1.setText("");
			jtf2.setText("");
			jtf3.setText("");
			jtf4.setText("");
			jtf5.setText("");
		}

	}
}
